package ua.eug.airv1;

import ua.eug.airv1.controllers.ScreenController;
import ua.eug.airv1.map.MapHolder;
import ua.eug.airv1.menu.MenuClickListener;
import ua.eug.airv1.menu.SideMenu;
import ua.eug.airv1.orders.OrderHolder;
import ua.eug.airv1.weapon.ExplosionPool;
import ua.eug.airv1.weapon.WeaponHolder;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;

public class BattleScreen implements Screen, MenuClickListener {

	private AirGame airGame;
	private Stage stage;
	private WeaponHolder weaponHolder;
	private ScreenController screenCtl;
	
	private int cnt = 0;
	
	static final String[] menuItems = {"menu_1", "menu_2", "menu_next", "menu_go"};

	public BattleScreen(AirGame airGame) {
		this.airGame = airGame;
		stage = new Stage();
		
		MapHolder mapHolder = MapHolder.getInstance();
		mapHolder.setTouchable(Touchable.enabled);
		stage.addActor(mapHolder);
				
		weaponHolder = WeaponHolder.getInstance();
		stage.addActor(weaponHolder);
		
		OrderHolder orders = OrderHolder.getInstance();
		stage.addActor(orders);

		mapHolder.loadMap();
		
		weaponHolder.test();
		
		ExplosionPool explosions = ExplosionPool.getInstance();
		stage.addActor(explosions);
		
		screenCtl = ScreenController.getInstance();
		if (Gdx.graphics.getWidth() > 1000)
			screenCtl.zoomUp();
		if (Gdx.graphics.getWidth() > 1300)
			screenCtl.zoomUp();
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		float sec = Math.min(Gdx.graphics.getDeltaTime(), 1 / 15f);
		stage.act(sec);
		screenCtl.act(sec); // for screen panning
		stage.draw();
		
		if(++cnt > 30) {
			cnt = 0;
			System.out.println("tick");
		}
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(stage);
		SideMenu menu = SideMenu.getInstance();
		menu.newMenu(menuItems, this );		
		stage.addActor(menu);
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public Boolean clicked(String name) {
		System.out.println(" clicked " + name);
		
		if (name.matches("menu_go")) {
			// Enable continuous rendering
			Gdx.graphics.setContinuousRendering(true);
			
			weaponHolder.executeOrders();
		}
		
		if (name.matches("menu_next")) {
			// Enable continuous rendering
			Gdx.graphics.setContinuousRendering(true);
			
			if ( !weaponHolder.getNextUnordered() )
				SideMenu.getInstance().setState("menu_next", false);
		}

		if (name.matches("menu_1")) { // test
			screenCtl.zoomUp();
		}
		if (name.matches("menu_2")) { // test
			screenCtl.zoomDown();
		}

		return true;
	}
}
